import requests

from simpleutil.web import parse_curl
kb_id = 78

post_curl="""
curl 'http://shy-kms.datagrand.cn/api/v1/knowledge_item/knowledge_details' \
  -H 'Accept: application/json, text/plain, */*' \
  -H 'Accept-Language: zh-CN,zh;q=0.9' \
  -H 'AppAuth: MTk5NjA4X2ttczAz' \
  -H 'Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE3MzQ5MTg1MzcsIm5iZiI6MTczNDkxODUzNywianRpIjoiMTkwMTRjN2YtYWY1Zi00Y2Q5LThmYjktNjQ3MmU1YmEzODMwIiwiZXhwIjoxNzM1NTIzMzM3LCJpZGVudGl0eSI6WyJ5dWZhbmciLG51bGxdLCJmcmVzaCI6ZmFsc2UsInR5cGUiOiJhY2Nlc3MiLCJ1c2VyX2NsYWltcyI6eyJ1c2VyaWQiOjIyLCJ1c2VybmFtZSI6Inl1ZmFuZyIsImxldmVsIjoiXHU2NjZlXHU5MDFhXHU3NTI4XHU2MjM3IiwidXNlcl9uaWNrbmFtZSI6Ilx1NGY1OVx1NjViOSIsInVzZXJfaWNvbiI6Ii4vYXNzZXRzL2ltYWdlcy9hdmF0YXIuc3ZnIiwidXNlcl9jZW50ZXJfaWQiOm51bGx9fQ.uIGinJoAFE_UP9Ivd2SrPUScCLwbZCJ3D_Bj9CvP3-Y' \
  -H 'Connection: keep-alive' \
  -H 'Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryXccoyWBqrcbsAEan' \
  -H 'KMS-TAG: kms' \
  -H 'Origin: http://shy-kms.datagrand.cn' \
  -H 'Referer: http://shy-kms.datagrand.cn/' \
  -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36 QuarkPC/1.10.5.187' \
  -H 'X-APP-ID: 199608' \
  -H 'X-APP-NAME: kms03' \
  -H 'X-SERVICE-TYPE: km' \
  --data-raw $'------WebKitFormBoundaryXccoyWBqrcbsAEan\r\nContent-Disposition: form-data; name="kb_id"\r\n\r\n78\r\n------WebKitFormBoundaryXccoyWBqrcbsAEan\r\nContent-Disposition: form-data; name="cate_parent_id"\r\n\r\n1989\r\n------WebKitFormBoundaryXccoyWBqrcbsAEan\r\nContent-Disposition: form-data; name="dir_type"\r\n\r\n0\r\n------WebKitFormBoundaryXccoyWBqrcbsAEan--\r\n' \
  --compressed \
  --insecure
"""

put_curl="""
curl 'http://shy-kms.datagrand.cn/api/v1/vpu/knowledge_item/knowledge_draft' \
  -H 'Accept: application/json, text/plain, */*' \
  -H 'Accept-Language: zh-CN,zh;q=0.9' \
  -H 'AppAuth: MTk5NjA4X2ttczAz' \
  -H 'Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE3MzQ5MTg1MzcsIm5iZiI6MTczNDkxODUzNywianRpIjoiMTkwMTRjN2YtYWY1Zi00Y2Q5LThmYjktNjQ3MmU1YmEzODMwIiwiZXhwIjoxNzM1NTIzMzM3LCJpZGVudGl0eSI6WyJ5dWZhbmciLG51bGxdLCJmcmVzaCI6ZmFsc2UsInR5cGUiOiJhY2Nlc3MiLCJ1c2VyX2NsYWltcyI6eyJ1c2VyaWQiOjIyLCJ1c2VybmFtZSI6Inl1ZmFuZyIsImxldmVsIjoiXHU2NjZlXHU5MDFhXHU3NTI4XHU2MjM3IiwidXNlcl9uaWNrbmFtZSI6Ilx1NGY1OVx1NjViOSIsInVzZXJfaWNvbiI6Ii4vYXNzZXRzL2ltYWdlcy9hdmF0YXIuc3ZnIiwidXNlcl9jZW50ZXJfaWQiOm51bGx9fQ.uIGinJoAFE_UP9Ivd2SrPUScCLwbZCJ3D_Bj9CvP3-Y' \
  -H 'Connection: keep-alive' \
  -H 'Content-Type: application/json' \
  -H 'KMS-TAG: kms' \
  -H 'Origin: http://shy-kms.datagrand.cn' \
  -H 'Referer: http://shy-kms.datagrand.cn/' \
  -H 'Use-Progress: NO' \
  -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36 QuarkPC/1.10.5.187' \
  -H 'X-APP-ID: 199608' \
  -H 'X-APP-NAME: kms03' \
  -H 'X-SERVICE-TYPE: km' \
  --data-raw '{"knowledge_id":2083,"name":"未命名","content":"[{\"children\":[{\"text\":\"xxxxxxx\"}],\"type\":\"p\",\"id\":\"5ioxp\"}]","kb_id":78,"cate_id":2083,"tag_ids":""}' \
  --compressed \
  --insecure
"""

approve_curl="""
curl 'http://shy-kms.datagrand.cn/api/v1/knowledge_approve/approve' \
  -H 'Accept: application/json, text/plain, */*' \
  -H 'Accept-Language: zh-CN,zh;q=0.9' \
  -H 'AppAuth: MTk5NjA4X2ttczAz' \
  -H 'Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE3MzQ5MTg1MzcsIm5iZiI6MTczNDkxODUzNywianRpIjoiMTkwMTRjN2YtYWY1Zi00Y2Q5LThmYjktNjQ3MmU1YmEzODMwIiwiZXhwIjoxNzM1NTIzMzM3LCJpZGVudGl0eSI6WyJ5dWZhbmciLG51bGxdLCJmcmVzaCI6ZmFsc2UsInR5cGUiOiJhY2Nlc3MiLCJ1c2VyX2NsYWltcyI6eyJ1c2VyaWQiOjIyLCJ1c2VybmFtZSI6Inl1ZmFuZyIsImxldmVsIjoiXHU2NjZlXHU5MDFhXHU3NTI4XHU2MjM3IiwidXNlcl9uaWNrbmFtZSI6Ilx1NGY1OVx1NjViOSIsInVzZXJfaWNvbiI6Ii4vYXNzZXRzL2ltYWdlcy9hdmF0YXIuc3ZnIiwidXNlcl9jZW50ZXJfaWQiOm51bGx9fQ.uIGinJoAFE_UP9Ivd2SrPUScCLwbZCJ3D_Bj9CvP3-Y' \
  -H 'Connection: keep-alive' \
  -H 'Content-Type: application/json' \
  -H 'KMS-TAG: kms' \
  -H 'Origin: http://shy-kms.datagrand.cn' \
  -H 'Referer: http://shy-kms.datagrand.cn/' \
  -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36 QuarkPC/1.10.5.187' \
  -H 'X-APP-ID: 199608' \
  -H 'X-APP-NAME: kms03' \
  -H 'X-SERVICE-TYPE: km' \
  --data-raw '{"knowledge_id":2083}' \
  --compressed \
  --insecure
"""

pass_approve_curl="""
curl 'http://shy-kms.datagrand.cn/api/v1/vpu/knowledge_approve/approve' \
  -H 'Accept: application/json, text/plain, */*' \
  -H 'Accept-Language: zh-CN,zh;q=0.9' \
  -H 'AppAuth: MTk5NjA4X2ttczAz' \
  -H 'Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE3MzQ5MTg1MzcsIm5iZiI6MTczNDkxODUzNywianRpIjoiMTkwMTRjN2YtYWY1Zi00Y2Q5LThmYjktNjQ3MmU1YmEzODMwIiwiZXhwIjoxNzM1NTIzMzM3LCJpZGVudGl0eSI6WyJ5dWZhbmciLG51bGxdLCJmcmVzaCI6ZmFsc2UsInR5cGUiOiJhY2Nlc3MiLCJ1c2VyX2NsYWltcyI6eyJ1c2VyaWQiOjIyLCJ1c2VybmFtZSI6Inl1ZmFuZyIsImxldmVsIjoiXHU2NjZlXHU5MDFhXHU3NTI4XHU2MjM3IiwidXNlcl9uaWNrbmFtZSI6Ilx1NGY1OVx1NjViOSIsInVzZXJfaWNvbiI6Ii4vYXNzZXRzL2ltYWdlcy9hdmF0YXIuc3ZnIiwidXNlcl9jZW50ZXJfaWQiOm51bGx9fQ.uIGinJoAFE_UP9Ivd2SrPUScCLwbZCJ3D_Bj9CvP3-Y' \
  -H 'Connection: keep-alive' \
  -H 'Content-Type: application/json' \
  -H 'KMS-TAG: kms' \
  -H 'Origin: http://shy-kms.datagrand.cn' \
  -H 'Referer: http://shy-kms.datagrand.cn/admin/' \
  -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36 QuarkPC/1.10.5.187' \
  -H 'X-APP-ID: 199608' \
  -H 'X-APP-NAME: kms03' \
  -H 'X-SERVICE-TYPE: km' \
  --data-raw '{"approve_id":1498,"approve_status":1,"deny_cause":""}' \
  --compressed \
  --insecure
"""


post_method,post_url,post_headers,post_data = parse_curl(
    post_curl,['Content-Type']
    );

put_method,put_url,put_headers,put_data = parse_curl(
    put_curl,[]
    );

approve_method,approve_url,approve_headers,approve_data = parse_curl(
    approve_curl,[]
    );

pass_approve_method,pass_approve_url,pass_approve_headers,pass_approve_data = parse_curl(
    pass_approve_curl,[]
    );

def post_rich_content_knowledge_detail(cate_parent_id:int):
    response = requests.post(
        url=post_url,
        headers=post_headers,
        # timeout=10,
        data={
            'kb_id': str(kb_id),
            'cate_parent_id': str(cate_parent_id),
            'dir_type': '0'
        })
    if response.status_code == 200 or response.status_code == 201:
        response_data = response.json()
        if response_data['status'] == 200:
            return response_data['data']['cate_id'], response_data['data'][
                'knowledge_id']
        else:
            print(f"Error: {response_data}")
            return None, None
    else:
        print(f"HTTP Error: {response.status_code}")
        return None, None


def put_rich_content_knowledge_detail(knowledge_id, name, content,
                                      cate_id):
    data = {
            "knowledge_id": int(knowledge_id),
            "name": name,
            "author": 'admin',
            "content": content,
            "kb_id": int(kb_id),
            "cate_id": int(cate_id),
            "tag_ids": ""
        }
    response = requests.put(
        url=put_url,
        headers=put_headers,
        timeout=10,
        json=data)
    if response.status_code == 200 or response.status_code == 201:
        response_data = response.json()
        if response_data['status'] == 200:
            print(f"Success: {name}")
            return True
        else:
            print(f"Error: {name}")
            print(f"Error: {response_data}")
            return False
    else:
        print(f"HTTP Error: {name}")
        print(f"HTTP Error: {response.status_code}")
        return False


def approve(knowledge_id):
    response = requests.post(
        url=approve_url,
        headers=approve_headers,
        timeout=10,
        json={"knowledge_id": knowledge_id})
    if response.status_code == 200 or response.status_code == 201:
        response_data = response.json()
        if response_data['status'] == 200:
            print("approve Success")
        else:
            print(f"Error: {response_data}")
    else:
        print(f"HTTP Error: {response.status_code}")

def pass_approve(approve_id):
    response = requests.post(
        url=pass_approve_url,
        headers=pass_approve_headers,
        timeout=10,
        json={
                "approve_id": approve_id,
                "approve_status": 1,
                "deny_cause": ""
            }
        )
    if response.status_code == 200 or response.status_code == 201:
        response_data = response.json()
        if response_data['status'] == 200:
            print("pass approve Success")
        else:
            print(f"Error: {response_data}")
    else:
        print(f"HTTP Error: {response.status_code}")